package main

import (
	"log"
	"net/http"
	"sync"
)

func init() {
	log.SetPrefix("TRACE: ")
	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
}

/*
waitGroup用来等待多个goroutine完成，main goroutine调用Add设置需要等待goroutine的数目，
每一个goroutine结束时调用Done(),Wait()被调用来等待所有的goroutine完成
*/

var (
	wg   sync.WaitGroup
	urls = []string{
		"http://qq.com",
		"https://new.qq.com/ch/photo/",
	}
)

func main() {
	for _, url := range urls {
		//每一个URL 启动一个 goroutine ，同时给wg 加1
		wg.Add(1)
		go func(url string) {
			defer wg.Done()
			resp, err := http.Get(url)
			if err == nil {
				log.Println(resp.Status)
			}
		}(url)

		//	等待所有请求结束
		wg.Wait()
	}

}
